package cn.leyaber.hrm.quartz.server.impl;


import cn.leyaber.hrm.quartz.MainJob;
import cn.leyaber.hrm.quartz.QuartzJobInfo;
import cn.leyaber.hrm.quartz.server.IQuartzService;
import cn.leyaber.hrm.quartz.server.QuartzUtils;
import com.alibaba.fastjson.JSONObject;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

@Service
public class QuartzServiceImpl implements IQuartzService {

   @Autowired
   private SchedulerFactoryBean schedulerFactory;

   @Override
   public void addJob(QuartzJobInfo info) {
      try {
         Scheduler sche = schedulerFactory.getScheduler();
         String infoJsonStr =  JSONObject.toJSONString(info);
         QuartzUtils.addJob(sche, info.getJobName(), MainJob.class, infoJsonStr, info.getCronj());
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   @Override
   public void delJob(String jobName) {
      try {
         Scheduler sche = schedulerFactory.getScheduler();
         QuartzUtils.removeJob(sche, jobName);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

}